define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'cuiban/lists/index' + location.search,
                    add_url: 'cuiban/lists/add',
                    // edit_url: 'cuiban/lists/edit',
                    del_url: 'cuiban/lists/del',
                    multi_url: 'cuiban/lists/multi',
                    import_url: 'cuiban/lists/import',
                    table: 'buchang_cuiban_list',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                fixedColumns: true,
                fixedRightNumber: 1,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'cuiban.car_num', title: __('Cuiban.car_num'), operate: 'LIKE'},
                        {field: 'cuiban.phone', title: __('Cuiban.phone'), operate: 'LIKE'},
                        {field: 'type', title: __('Type'),searchList: {
                                1: __('报案'),
                                2: __('咨询'),
                                3: __('退费'),
                                4: __('理赔'),
                                5: __('催办'),
                                6: __('投诉'),
                            },
                            custom: {
                                0: 'primary',
                                1: 'default',
                                2: 'warning',
                                3: 'info',
                                4: 'huang',
                                5: 'success',
                                6: 'danger',
                            },formatter: Table.api.formatter.label},
                        {field: 'user_name', title: __('User_name'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        {field: 'date', title: __('录入时间'), operate:'like', formatter: Table.api.formatter.content},
                        {field: 'content', title: __('Content'),operate: false,formatter: Table.api.formatter.content},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'updatetime', title: __('Updatetime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        // {field: 'user_id', title: __('User_id')},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate,
                            buttons: [
                                {
                                    name: 'add',
                                    text: __('继续录入'), // 按钮文字，语言在对应的lang文件设置
                                    title: __('继续录入'), // 提示
                                    classname: 'btn btn-xs btn-success btn-dialog',
                                    icon: '', // 更换图标
                                    url: 'cuiban/buchang/add/ids/{bid}',
                                    extend: 'data-area=["100%","100%"]',
                                },
                                {
                                    name: 'del',
                                    text: __('删除'), // 按钮文字，语言在对应的lang文件设置
                                    title: __('删除'), // 提示
                                    classname: 'btn btn-xs btn-danger btn-ajax',
                                    icon: '', // 更换图标
                                    url: 'cuiban/lists/del',
                                    confirm: '确认删除吗？',
                                    success: function (data, ret) {
                                        table.bootstrapTable('refresh');
                                    },
                                    visible: function (row) {
                                        //权限判断
                                        if (Config.rule.includes('458') || Config.rule.includes(458)) {
                                            //返回true时按钮显示,返回false隐藏
                                            return true;
                                        }
                                    }
                                }
                            ]
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        recyclebin: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    'dragsort_url': ''
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: 'cuiban/lists/recyclebin' + location.search,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {
                            field: 'deletetime',
                            title: __('Deletetime'),
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            formatter: Table.api.formatter.datetime
                        },
                        {
                            field: 'operate',
                            width: '140px',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            buttons: [
                                {
                                    name: 'Restore',
                                    text: __('Restore'),
                                    classname: 'btn btn-xs btn-info btn-ajax btn-restoreit',
                                    icon: 'fa fa-rotate-left',
                                    url: 'cuiban/lists/restore',
                                    refresh: true
                                },
                                {
                                    name: 'Destroy',
                                    text: __('Destroy'),
                                    classname: 'btn btn-xs btn-danger btn-ajax btn-destroyit',
                                    icon: 'fa fa-times',
                                    url: 'cuiban/lists/destroy',
                                    refresh: true
                                }
                            ],
                            formatter: Table.api.formatter.operate
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },

        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
